package public

import (
	"fmt"
	"github.com/gin-gonic/contrib/sessions"
	"github.com/gin-gonic/gin"
	"math/rand"
	"time"
)
func RandCode() int32 {
	strings := rand.New(rand.NewSource(time.Now().UnixNano())).Int31n(1000000)
	return strings
}
//mobilde=电话 use=用途 reture=验证码 code=验证码
func SetCode(mobile string, use string,code int32,c *gin.Context) {
	times := time.Now().Unix()
	session :=sessions.Default(c)
	session.Set(mobile+"card"+use,code)
	session.Set(mobile+"time"+use,times)
	session.Save()
}
//true有效 false无效
func GetCode(mobile string,i string,code int32,c *gin.Context) (string,bool){
	s:=sessions.Default(c)
	user:=s.Get(mobile+"card"+i)
	if user == "" {
		return "获取失败",false
	}
	times:=s.Get(mobile+"time"+i)
	if times==nil{
		return "验证码不正确",false
	}
	fmt.Printf("%v %v \n",user,code)
	if user != code {
		return "验证码不正确",false
	}
	limit:=time.Now().Unix()-times.(int64)
	if limit>60{
		return "验证码超时",false
	}
	return "验证成功",true
}